<template>
	<yb-list
	ref="list"
	type="page"
	custom-class="yb-flex-1"
	:bounce="bounce"
	:empty="emptySync"
	:loading="loadingSync"
	:pulldown="pulldownSync"
	:loadmore="loadmoreSync"
	:enableScrolling="enableScrolling"
	@scroll="onScroll"
	@pulldown="onPulldown"
	@loadmore="onLoadmore">
		<slot></slot>
		<yb-list-item v-for="(item, index) in listSync" :key="item.id + '_' + index + '_' + item.type">
			<view class="rd-community padding-gap" @tap="app.$business.openDetail(item)" :style="{
				'border-bottom-color': skinColor.color_gap_1
			}">
				<view class="subtitle">
					<yb-avatar class="cover" :size="80" :src="item.avatar || app.$business.getDefaultAvatar(item.source)"></yb-avatar>
					<view class="info yb-flex">
						<yb-text custom-class="title" :lines="1" :size="30" :color="skinColor.color_1" :value="item.creator || app.$business.filterSource(item.source).title"></yb-text>
						<yb-text custom-class="time" :lines="1" :size="24" :color="skinColor.color_3" :value="item.updateTime"></yb-text>
					</view>
					<yb-tag plain v-if="app.$business.getType(item.type)" :options="{
						bgColor: skinColor.color_1
					}" :value="app.$business.getType(item.type) + '推荐'"></yb-tag>
				</view>
				<view class="content" v-if="desc(item)">
					<text class="content-text" :style="{color: skinColor.color_1}">{{desc(item)}}</text>
				</view>
				<view class="tags yb-flex yb-row yb-wrap">
					<yb-tap stop @click="app.$business.showTaglist(tag, item.type)" v-for="(tag, i) in item.tag" :key="i">
						<yb-tag :options="{
							bgColor: skinColor.color_3,
							color: skinColor.color_white
						}" custom-class="tag" :value="tag"></yb-tag>
					</yb-tap>
				</view>
				<view class="images">
					<template v-if="item.list.length > 0">
						<yb-grid :data="item.list" :gap="columns(item) == 2 ? '30' : columns(item) == 3 ? '20' : 0" :column="columns(item)" :count="6">
							<template v-for="(image, i) in item.list" :slot="i">
								<yb-tap stop @click="onItemClick(image)">
									<rd-image lazyLoad :borderRadius="12" :class="'image_' + columns(item)" :mode="columns(item) == 1 ? 'heightFix' : 'aspectFill'" :src="image.type ? app.$config.characterTypes.single[image.type] ? (image.avatar || app.$business.filterSource(image.source).logo || '/static/logo.png') : app.$business.getCover(image) : image"></rd-image>
								</yb-tap>
								<text v-if="image.type" class="badge" :style="{
									'background-color': app.$business.getColor(image.type),
									color: skinColor.color_white
								}">{{app.$business.getType(image.type)}}</text>
							</template>
						</yb-grid>
					</template>
					<template v-else-if="item.cover">
						<rd-image lazyLoad :border-radius="10" class="image_1" :src="item.cover" mode="heightFix"></rd-image>
					</template>
				</view>
				<view class="bottom">
					<text class="num" :style="{color: skinColor.color_2}">浏览次数：{{app.$utils.numtounit(item.readnum) || 0}}</text>
					<view class="actions">
						<view class="action">
							<rd-icon name="fire" size="30" :color="skinColor.color_3"></rd-icon>
							<text class="text" :style="{color: skinColor.color_3}">{{app.$utils.numtounit(item.popular)}}</text>
						</view>
						<view class="gap"></view>
						<view class="action">
							<rd-icon name="thumbs" size="30" :color="skinColor.color_3"></rd-icon>
							<text class="text" :style="{color: skinColor.color_3}">{{app.$utils.numtounit(item.thumbs)}}</text>
						</view>
						<view class="gap"></view>
						<view class="action">
							<rd-icon name="comment" size="30" :color="skinColor.color_3"></rd-icon>
							<text class="text" :style="{color: skinColor.color_3}">{{app.$utils.numtounit(item.comment)}}</text>
						</view>
						<view class="gap"></view>
						<view class="action">
							<rd-icon name="love" size="30" :color="skinColor.color_3"></rd-icon>
							<text class="text" :style="{color: skinColor.color_3}">{{app.$utils.numtounit(item.collect)}}</text>
						</view>
					</view>
				</view>
			</view>
			<slot name="bottom"></slot>
		</yb-list-item>
	</yb-list>
</template>

<script>
	import appMixin from '@/common/mixin/app.js';
	export default {
		mixins: [appMixin],
		props: {
			list: {
				type: Array,
				default () {
					return new Array
				}
			},
			enableScrolling: {
				type: Boolean,
				default: false
			},
			bounce: {
				type: Boolean,
				default: true
			},
			index: {
				type: [Number,String],
				default: -1
			},
			loading: {
				type: [Boolean,Object,String],
				default: false
			},
			empty: {
				type: [Boolean,Object,String],
				default: false
			},
			pulldown: {
				type: [Boolean,Object],
				default: false
			},
			loadmore: {
				type: [Boolean,Object],
				default: false
			}
		},
		computed: {
			emptySync () {
				return Object.assign({}, {
					show: false,
					text: '暂无数据',
					color: this.skinColor.color_gap_1,
					bgColor: this.skinColor.color_bg_1
				}, this.app.$utils.typeof(this.empty) == 'String' ? {text: this.empty} : this.app.$utils.typeof(this.empty) == 'Boolean' ? {show: this.empty} : this.empty)
			},
			loadingSync () {
				return Object.assign({}, {
					show: false,
					text: '加载中',
					color: this.skinColor.color_1,
					bgColor: this.skinColor.color_bg_1
				}, this.app.$utils.typeof(this.loading) == 'String' ? {text: this.loading} : this.app.$utils.typeof(this.loading) == 'Boolean' ? {show: this.loading} : this.loading)
			},
			pulldownSync () {
				return Object.assign({}, {
					show: false,
					color: this.skinColor.color_1
				}, this.app.$utils.typeof(this.pulldown) == 'Boolean' ? {show: this.pulldown} : this.pulldown)
			},
			loadmoreSync () {
				return Object.assign({}, {
					show: !this.isLastPage,
					color: this.skinColor.color_1
				}, this.app.$utils.typeof(this.loadmore) == 'Boolean' ? {show: this.loadmore} : this.loadmore)
			},
			listSync () {
				return this.list.slice(0, Math.min(this.page * this.size, this.list.length))
			},
			isLastPage () {
				return this.page * this.size >= this.list.length
			}
		},
		data () {
			return {
				page: 1,
				size: 10,
				isLoadmoreEnd: false
			}
		},
		methods: {
			desc (params) {
				if ( this.app.$config.communityTypes.single[params.type] ) {
					return params.desc.trim() || params.title.trim()
				} else if ( this.app.$config.characterTypes.single[params.type] ) {
					return `昵称：${params.name.trim()}\n简介：${params.desc.trim() || '这个人很懒什么都没有留下'}`
				} else {
					return `标题：${params.title.trim()}${params.latest ? '\n更新到：' + params.latest.trim() : ''}\n简介：${params.desc.trim() || '暂无简介'}`
				}
			},
			columns (params) {
				return params.list?.length == 1 ? 1 : (params.list?.length == 2 || params.list?.length == 4) ? 2 : 3
			},
			onItemClick (params) {
				if ( params.type ) {
					this.app.$business.openDetail(params)
				} else {
					this.app.$business.previewImage({
						urls: [params]
					})
				}
			},
			onScroll (e) {
				this.$emit('scroll', e)
			},
			onPulldown (callback) {
				this.page = 1
				this.isLoadmoreEnd = false
				if ( this.index > -1 ) {
					this.$emit('pulldown', this.index, callback)
				} else {
					this.$emit('pulldown', callback)
				}
			},
			onLoadmore (callback) {
				if ( this.isLastPage ) {
					if ( this.page * this.size == this.listSync.length ) {
						this.page++
					} 
					if ( this.index > -1 ) {
						this.$emit('loadmore', this.index, callback)
					} else {
						this.$emit('loadmore', callback)
					}
				} else {
					this.page++
					callback('success')
				}
			},
			resetLoadmore () {
				this.isLoadmoreEnd = false
				this.$refs.list.resetLoadmore()
			},
			backTop () {
				this.$refs.list.backTop()
			},
			setLoadmoreEnd () {
				this.isLoadmoreEnd = true
				if ( this.isLastPage ) this.$refs.list.setLoadmoreEnd()
			},
			setLoadmoreSuccess() {
				this.isLoadmoreEnd = false
				if ( this.isLastPage ) this.$refs.list.setLoadmoreSuccess()
			},
			setLoadmoreFail() {
				this.isLoadmoreEnd = false
				if ( this.isLastPage ) this.$refs.list.setLoadmoreFail()
			}
		},
		watch: {
			isLastPage (newVal) {
				if ( !newVal ) {
					this.$refs.list && this.$refs.list.setLoadmoreSuccess()
				} else {
					if ( this.isLoadmoreEnd ) {
						this.$refs.list && this.$refs.list.setLoadmoreEnd()
					}
				}
			},
			list (newVal) {
				if ( newVal.length < this.page * this.size ) {
					this.page = Math.ceil(newVal.length / 20) > 0 ? Math.ceil(newVal.length / 20) : 1
				}
			}
		}
	}
</script>

<style>
	.rd-community {
		padding: 30rpx 0;
		border-bottom-width: 1px;
		border-bottom-style: solid;
	}
	.rd-community .subtitle {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		margin-bottom: 20rpx;
	}
	.rd-community .subtitle .cover {
		margin-right: 20rpx;
	}
	.rd-community .subtitle .info {
		flex: 1;
	}
	.rd-community .subtitle .info .time {
		margin-top: 10rpx;
	}
	.rd-community .content {
		margin-bottom: 20rpx;
	}
	.rd-community .content .btn {
		font-size: 26rpx;
		border-bottom-width: 1px;
		border-bottom-style: solid;
		width: 104rpx;
		padding: 10rpx 0 5rpx 0;
	}
	.rd-community .content-text {
		font-size: 28rpx;
		line-height: 40rpx;
		lines: 5;
		text-overflow: ellipsis;
	}
	.rd-community .tags {
		margin-bottom: 10rpx;
	}
	.rd-community .tags .tag {
		margin-right: 10rpx;
		margin-bottom: 10rpx;
	}
	.rd-community .images {
		margin-bottom: 20rpx;
	}
	.rd-community .images .image_1 {
		height: 450rpx;
	}
	.rd-community .images .image_2 {
		height: 335rpx;
	}
	.rd-community .images .image_3 {
		height: 220rpx;
	}
	.rd-community .images .badge {
		font-size: 18rpx;
		position: absolute;
		right: 0;
		top: 0;
		padding: 5rpx 15rpx 5rpx 10rpx;
		border-bottom-right-radius: 10rpx;
		border-top-left-radius: 10rpx;
	}
	.rd-community .bottom {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
	.rd-community .bottom .num {
		font-size: 26rpx;
		flex: 1;
		lines: 1;
		text-overflow: ellipsis;
	}
	.rd-community .bottom .actions {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-end;
		flex: 1;
	}
	.rd-community .bottom .actions .action {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
	.rd-community .bottom .actions .gap {
		width: 50rpx;
	}
	.rd-community .bottom .actions .action .text {
		font-size: 24rpx;
		margin-left: 5rpx;
	}
</style>